package com.api.oa.mapper;

import com.api.oa.model.entity.YQInsEntity;
import com.api.oa.model.vo.OaNoticeVo;
import com.api.oa.model.vo.YQInsEntityVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface YQInsEntityMapper {

    @Insert("insert into  yq_ins  (" +
        "insName," +
        "insCode," +
        "careType," +
        "contacts," +
        "fixedPhone," +
        "mobilePhone," +
        "insAddress," +
        "lat," +
        "lng," +
        "shopImage," +
        "districtRouter," +
        "openTime," +
        "insInfo," +
        "disabled," +
        "location," +
        "createdAt" +
    ") " +
    "values " +
    "(" +
        "#{entity.insName}," +
        "#{entity.insCode}," +
        "#{entity.careType}," +
        "#{entity.contacts}," +
        "#{entity.fixedPhone}," +
        "#{entity.mobilePhone}," +
        "#{entity.insAddress}," +
        "#{entity.lat}," +
        "#{entity.lng}," +
        "#{entity.shopImage}," +
        "#{entity.districtRouter}," +
        "#{entity.openTime}," +
        "#{entity.insInfo}," +
        "#{entity.disabled}," +
        "#{entity.location}," +
        "#{entity.createdAt}" +
    ")" )
    //@Options(useGeneratedKeys = true, keyProperty = "noticeId", keyColumn = "id")
    Integer insert(@Param("entity") YQInsEntity entity);



    @Update("update  yq_ins  set " +
        "insName = #{entity.insName}," +
        "insCode = #{entity.insCode}," +
        "careType = #{entity.careType}," +
        "contacts = #{entity.contacts}," +
        "fixedPhone = #{entity.fixedPhone}," +
        "mobilePhone = #{entity.mobilePhone}," +
        "insAddress = #{entity.insAddress}," +
        "districtRouter = #{entity.districtRouter}," +
        "lat = #{entity.lat}," +
        "lng = #{entity.lng}," +
        "shopImage = #{entity.shopImage}," +
        "openTime = #{entity.openTime}," +
        "insInfo = #{entity.insInfo}," +
        "location = #{entity.location}," +
        "disabled = #{entity.disabled}" +
    " where insCode = #{entity.insCode}")
    Integer update(@Param("entity") YQInsEntity  entity);


    @Delete("delete from yq_ins where insCode=#{id}")
    void delete(@Param("id") String id);


    @Select("select yq_ins.*,yq_user.userId from yq_ins left join yq_user on yq_ins.insCode = yq_user.insCode  where yq_ins.insCode = #{id}")
    List<YQInsEntityVO> list(@Param("id") Long pid);


    @Select("select * from yq_ins   where insCode = #{id} limit 1")
    YQInsEntity findOne(@Param("id") String id);

    @Select("select * from v_yq_ins_user   where userId = #{id} limit 1")
    YQInsEntity findOneByUserId(@Param("id") Long id);


    @Select("select count(insCode) from  yq_ins  where insCode = #{insCode} limit 1")
    Integer isExit(@Param("insCode")String id);


    @Select("<script>select v_yq_ins.*,oa_org.orgName from v_yq_ins left join oa_org on v_yq_ins.careType = oa_org.orgId  where 1=1 " +
            "<if test='insCode!=null and insCode!=\"\"'> and concat(ifnull(v_yq_ins.insCode,''),ifnull(v_yq_ins.insName,'')) like concat('%', #{insCode},'%') </if>" +
            "<if test='careType!=null and careType!=\"\"'> and v_yq_ins.careType = #{careType}) </if>" +
            "</script>")
    List<YQInsEntityVO> page(@Param("insCode") String insCode,@Param("careType") String careType);


    @Select("<script>select * from v_yq_ins_user  <where>  <if test='insName!=null and insName!=\"\"'> concat(ifnull(insName,''), ifnull(insCode,''))  like concat('%', #{insName},'%') </if></where></script>")
    List<YQInsEntityVO> search(@Param("insName") String insName);


    @Select("<script>select * from v_yq_ins_user  where insCode = #{insCode} limit 1 </script>")
    YQInsEntityVO findOneByInsCode(@Param("insCode") String insCode);


    @Select("<script> select count(*) from yq_ins where 1=1 <if test='insCode!=null and insCode!=\"\"'>and insCode =#{insCode} </if> </script>")
    Long count(@Param("insCode") String insCode);

    @Select("<script>select v_yq_ins.* from v_yq_ins where 1=1 " +
            "<if test='orgId!=null and orgId!=\"\"'> and v_yq_ins.careType = #{orgId}) </if>" +
            "</script>")
    List<YQInsEntityVO> listInsByOrgId(@Param("orgId") String id);
}

